package org.marsdonne.audits;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.management.ManagementFactory;

public interface ExceptionAuditable {
    void captured(Caught caught);

    default void captured(Throwable exception) {
        Caught caught = new Caught(exception.getClass().getName());

        StringWriter stackWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stackWriter);
        exception.printStackTrace(printWriter);
        caught.setStackTrace(stackWriter.toString());

        caught.setProcessId(ManagementFactory.getRuntimeMXBean().getName());

        this.captured(caught);
    }
}
